home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 12.8 KB | 382 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWMemMgr.h
- // Release Version: $ 1.0d1 $
- //
- // Creation Date: 3/28/94
- //
- // Copyright: © 1993, 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef FWMEMMGR_H
- #define FWMEMMGR_H
-
- #include <stddef.h>
-
- #ifndef FWSTDDEF_H
- #include "FWStdDef.h"
- #endif
-
- #ifndef FWPRIEXC_H
- #include "FWPriExc.h"
- #endif
-
- #ifndef FWEXCDEF_H
- #include "FWExcDef.h"
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__MEMORY__)
- #include <Memory.h>
- #endif
-
- #if defined(FW_BUILD_WIN32S) && !defined(_INC_WINDOWS)
- #include <Windows.h>
- // API macro that conflicts with a method in FW_CMemoryManager
- // Why Microsoft could not do this with inlines, remains a mystery to this date...
- #undef CopyMemory
- #endif
-
- //========================================================================================
- // Forward class declarations
- //========================================================================================
-
- class FW_CMemoryHeap;
-
- //========================================================================================
- // Type definitions
- //========================================================================================
-
- typedef void (*FW_PFVV)();
- // Pointer to function returning void.
- // Used with set_new_handler. See ARM, pp 280-81.
-
- //========================================================================================
- // Global procedure definitions
- //========================================================================================
-
- #ifdef FW_NEW_AND_DELETE_NOT_DISABLED
-
- // Currently parts must use OpenDoc's new and delete to avoid mismatching new and delete
- // calls in certain situations. Will be fixed soon.
-
- #ifdef __BORLANDC__
- void* cdecl operator new(size_t size);
- void cdecl operator delete(void* block);
- #else
- void* operator new(size_t size);
- void operator delete(void* block);
- #endif
-
- #endif
-
- FW_PFVV set_new_handler(FW_PFVV handler);
- // See ARM, pp 280-81.
-
- #ifdef FW_BUILD_MAC
- pascal long MacGrowZoneProc(Size /* cbNeeded */);
-
- void SetStackSpace(long numBytes);
-
- pascal Ptr GetCurrentStackBase() = { /* kMoveLAbsolute */ 0x2eb8,
- /* CurStackBase */ 0x0908 };
- /* MOVE.L CurStackBase,(SP) */
- #endif
-
- //========================================================================================
- // CLASS FW_CMemoryManager
- //========================================================================================
-
- class FW_CMemoryManager
- {
- public:
-
- // Utility routines for manipulating blocks of memory
- static void CopyMemory(const void* const source,
- void* const destination,
- unsigned long bytesToMove);
- static void SetMemory(void* aBlock,
- unsigned long bytesToSet,
- unsigned char byteValue);
- static void* AddOffsetToPointer(void* pointer,
- unsigned long Offset);
-
- // when resizable, pointer-based blocks are needed:
- static void* AllocateBlock(unsigned long bytesRequested);
- static void* ResizeBlock(void* aBlock,
- unsigned long bytesRequested);
- static void FreeBlock(void* aBlock);
- static unsigned long GetBlockSize(void* aBlock);
-
- // when platform specific handles are needed:
- static FW_PlatformHandle AllocateSystemHandle(unsigned long bytesRequested);
- static FW_PlatformHandle ResizeSystemHandle(FW_PlatformHandle aHandle,
- unsigned long bytesRequested);
- static void FreeSystemHandle(FW_PlatformHandle aHandle);
- static void* LockSystemHandle(FW_PlatformHandle aHandle);
- static void UnlockSystemHandle(FW_PlatformHandle aHandle);
- static unsigned long GetSystemHandleSize(FW_PlatformHandle aHandle);
- static FW_PlatformHandle CopySystemHandle(FW_PlatformHandle aHandle);
-
- //internal methods
- // when resizable, pointer-based blocks are needed, no debug checking:
- static void* PrimitiveAllocateBlock(unsigned long bytesRequested);
- static void* PrimitiveResizeBlock(void* aBlock,
- unsigned long bytesRequested);
- static void PrimitiveFreeBlock(void* aBlock);
- static unsigned long PrimitiveGetBlockSize(void* aBlock);
-
- static void InitializeRawBlock(void* aBlock,
- unsigned long sizeBlock);
-
- static void DefaultNewHandler();
-
- private:
- static FW_CMemoryHeap* GetMemoryHeap();
-
- FW_CMemoryManager() {};
- // abstract class. Note that all methods are statics.
- };
-
-
- //========================================================================================
- // CLASS FW_XMemoryExhausted
- //
- // An exception class for free store exhausted errors.
- //========================================================================================
-
- // Work In Progress: ???JEL
- // Some names are too long. I suggest the following name changes:
- //
- // fSizeRequestedAvailability -> fHasSizeRequested
- // SizeRequestedAvailability -> FW_Boolean
- //
- // See the inline for FW_XMemoryExhausted::HasSizeRequested below
-
- class FW_XMemoryExhausted : public FW_XPrivException
- {
- public:
- enum SizeRequestedAvailability
- {
- kSizeRequestedNotAvailable,
- kSizeRequestedAvailable
- };
- FW_XMemoryExhausted();
- FW_XMemoryExhausted(unsigned long requestedSize);
- FW_XMemoryExhausted(const FW_XMemoryExhausted& exception);
- virtual ~FW_XMemoryExhausted();
- unsigned long GetRequestedSize() const;
- SizeRequestedAvailability HasSizeRequested() const;
-
- _FW_EXCEPTION_DEFINE(FW_XMemoryExhausted)
- private:
- const unsigned long fRequestedSize;
- const SizeRequestedAvailability fSizeRequestedAvailability;
- };
-
- //========================================================================================
- // FW_XMemoryExhausted inline member functions
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_XMemoryExhausted::FW_XMemoryExhausted
- //----------------------------------------------------------------------------------------
-
- inline FW_XMemoryExhausted::FW_XMemoryExhausted() :
- FW_XPrivException(),
- fSizeRequestedAvailability(kSizeRequestedNotAvailable),
- fRequestedSize(0)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_XMemoryExhausted::FW_XMemoryExhausted
- //----------------------------------------------------------------------------------------
-
- inline FW_XMemoryExhausted::FW_XMemoryExhausted(unsigned long requestedSize) :
- FW_XPrivException(),
- fSizeRequestedAvailability(kSizeRequestedAvailable),
- fRequestedSize(requestedSize)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_XMemoryExhausted::FW_XMemoryExhausted
- //----------------------------------------------------------------------------------------
-
- inline FW_XMemoryExhausted::FW_XMemoryExhausted(const FW_XMemoryExhausted& exception) :
- FW_XPrivException(),
- fSizeRequestedAvailability(exception.fSizeRequestedAvailability),
- fRequestedSize(exception.fRequestedSize)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_XMemoryExhausted::GetRequestedSize
- //----------------------------------------------------------------------------------------
-
- inline unsigned long FW_XMemoryExhausted::GetRequestedSize() const
- {
- return fRequestedSize;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_XMemoryExhausted::HasSizeRequested
- //----------------------------------------------------------------------------------------
-
- inline FW_XMemoryExhausted::SizeRequestedAvailability FW_XMemoryExhausted::HasSizeRequested() const
- {
- return fSizeRequestedAvailability;
- }
-
- //========================================================================================
- // CLASS FW_CAcquireLockedSystemHandle
- //
- // A resource acquisition helper object for locking a system handle within a scope.
- //========================================================================================
-
- template<class tItem>
- class FW_CAcquireLockedSystemHandle : public _FW_CAutoDestructObject
- {
- public:
- FW_CAcquireLockedSystemHandle(FW_PlatformHandle aSystemHandle);
- virtual ~FW_CAcquireLockedSystemHandle();
-
- tItem* GetPointer() const;
-
- private:
- FW_CAcquireLockedSystemHandle(const FW_CAcquireLockedSystemHandle<tItem>& acquireObject);
- FW_CAcquireLockedSystemHandle<tItem>& operator=(const FW_CAcquireLockedSystemHandle<tItem>& acquireObject);
- // Copy constructor and assignment operator not valid for this class.
-
- FW_PlatformHandle fLockedSystemHandle;
- tItem* fLockedPointer;
- };
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireLockedSystemHandle::FW_CAcquireLockedSystemHandle
- //----------------------------------------------------------------------------------------
- template<class tItem>
- FW_CAcquireLockedSystemHandle<tItem>::FW_CAcquireLockedSystemHandle(FW_PlatformHandle aSystemHandle)
- : fLockedSystemHandle(aSystemHandle),
- fLockedPointer((tItem*)FW_CMemoryManager::LockSystemHandle(aSystemHandle))
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireLockedSystemHandle::~FW_CAcquireLockedSystemHandle
- //----------------------------------------------------------------------------------------
- template<class tItem>
- FW_CAcquireLockedSystemHandle<tItem>::~FW_CAcquireLockedSystemHandle()
- {
- FW_START_DESTRUCTOR
- FW_CMemoryManager::UnlockSystemHandle(fLockedSystemHandle);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireLockedSystemHandle::GetPointer
- //----------------------------------------------------------------------------------------
- template<class tItem>
- tItem* FW_CAcquireLockedSystemHandle<tItem>::GetPointer() const
- {
- return fLockedPointer;
- }
-
- //========================================================================================
- // CLASS FW_CAcquireTemporarySystemHandle
- //
- // A resource acquisition helper object for allocating and locking a system handle
- // within a scope. You can transfer ownership of the handle by setting fFree to FALSE.
- //========================================================================================
-
- template<class tItem>
- class FW_CAcquireTemporarySystemHandle : public _FW_CAutoDestructObject
- {
- public:
- FW_CAcquireTemporarySystemHandle(unsigned long size);
- virtual ~FW_CAcquireTemporarySystemHandle();
-
- void Resize(unsigned long newSize);
-
- FW_PlatformHandle fTemporarySystemHandle;
- tItem* fMemoryPointer;
-
- FW_Boolean fFree; // If TRUE, the dtor will free the fMemory
-
- private:
- FW_CAcquireTemporarySystemHandle(const FW_CAcquireTemporarySystemHandle<tItem>& acquireObject);
- FW_CAcquireTemporarySystemHandle<tItem>& operator=(const FW_CAcquireTemporarySystemHandle<tItem>& acquireObject);
- // Copy constructor and assignment operator not valid for this class.
- };
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireTemporarySystemHandle::FW_CAcquireTemporarySystemHandle
- //----------------------------------------------------------------------------------------
-
- template<class tItem>
- FW_CAcquireTemporarySystemHandle<tItem>::FW_CAcquireTemporarySystemHandle(unsigned long size) :
- fTemporarySystemHandle(FW_CMemoryManager::AllocateSystemHandle(size))
- {
- FW_TRY
- {
- fMemoryPointer = (tItem*) FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
- fFree = TRUE;
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- FW_CMemoryManager::FreeSystemHandle(fTemporarySystemHandle);
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireTemporarySystemHandle::~FW_CAcquireTemporarySystemHandle
- //----------------------------------------------------------------------------------------
-
- template<class tItem>
- FW_CAcquireTemporarySystemHandle<tItem>::~FW_CAcquireTemporarySystemHandle(void)
- {
- FW_START_DESTRUCTOR
-
- FW_CMemoryManager::UnlockSystemHandle(fTemporarySystemHandle);
-
- if(fFree)
- FW_CMemoryManager::FreeSystemHandle(fTemporarySystemHandle);
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireTemporarySystemHandle::Resize
- //----------------------------------------------------------------------------------------
-
- template<class tItem>
- void FW_CAcquireTemporarySystemHandle<tItem>::Resize(unsigned long newSize)
- {
- // unlock the handle before resizing
- FW_CMemoryManager::UnlockSystemHandle(fTemporarySystemHandle);
-
- // then resize it
- FW_TRY
- {
- fTemporarySystemHandle =
- FW_CMemoryManager::ResizeSystemHandle(fTemporarySystemHandle, newSize);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- // relock the handle
- FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
- }
-
- #endif
-